<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @submit.native.prevent="dataFormSubmit()" label-width="80px">
    <el-form-item label="商铺分类" prop="name">
      <el-select v-model="dataForm.userClassId" placeholder="商铺分类">
        <el-option
          v-for="item in userIdOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="模版图片" prop="imgName">
      <el-upload
        class="avatar-uploader"
        :action="this.$http.adornUrl(`/shopTemplate/uploadIcon?token=${this.$cookie.get('token')}`)"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="this.dataForm.imgName" :src="'https://image.yilianbaihui.cn/consumer/' + this.dataForm.imgPath + '/' + this.dataForm.imgName" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item label="模版名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="模版名称"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input type="number" v-model="dataForm.sort" placeholder="排序" maxlength="10"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remarks">
      <el-input type="textarea" v-model="dataForm.remarks" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="describe">
      <UEditor ref="ueditor"></UEditor>
      <!-- <el-input type="textarea" v-model="dataForm.describe" placeholder="描述"></el-input> -->
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import UEditor from '@/components/ueditor/ueditor.vue'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userClassId: '',
          name: '',
          remarks: '',
          describe: '',
          sort: '',
          imgPath: '',
          imgName: '',
        },
        userIdOptions: [{
          value: 373,
          label: '生鲜水果'
        }, {
          value: 374,
          label: '美食饮品'
        }, {
          value: 375,
          label: '生活服务'
        }, {
          value: 376,
          label: '丽人美妆'
        }, {
          value: 377,
          label: '家具建材'
        }, {
          value: 378,
          label: '超市便利'
        }, {
          value: 381,
          label: '休闲娱乐'
        }],
        dataRule: {
          userClassId: [
            { required: true, message: '店铺分类不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '模版名称不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      UEditor
    },
    methods: {
      handleAvatarSuccess(res, file) {
        debugger
        let path = []
        path = res.data[0].urlList[0].split('/')
        this.dataForm.imgPath = path[0]
        this.dataForm.imgName = path[1]
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg' || 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 1;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 jpg、png 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 1MB!');
        }
        return isJPG && isLt2M;
      },
      init (id) {
        this.dataForm.id = id || 0
        this.dataForm.imgPath = ''
        this.dataForm.imgName = ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/shopTemplate/getShopTemplateInfoById/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userClassId = data.data.userClassId
                this.dataForm.name = data.data.name
                this.dataForm.remarks = data.data.remarks
                this.$refs.ueditor.setUEContent('')
                this.$refs.ueditor.setUEContent(data.data.describe)
                // this.dataForm.describe = data.data.describe
                this.dataForm.sort = data.data.sort
                this.dataForm.imgPath = data.data.imgPath
                this.dataForm.imgName = data.data.imgName
              }
            })
          } else {
            this.$refs.ueditor.setUEContent('')
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        if (this.dataForm.imgName === '' ||this.dataForm.imgName == null) {
          this.$message.error('请上传图片文件！')
          return
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shopTemplate/${!this.dataForm.id ? 'addShopTemplate' : 'modifyShopTemplateById'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userClassId': this.dataForm.userClassId,
                'name': this.dataForm.name,
                'remarks': this.dataForm.remarks,
                'describe': this.$refs.ueditor.getUEContent(),
                'sort': this.dataForm.sort,
                'imgPath': this.dataForm.imgPath,
                'imgName': this.dataForm.imgName 
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
